/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t_cq_city_payment_detect.c                         */
/*  NOTE      =                                                    */
/*  DATE      = 2017/03/24 by jikw                                 */
/*******************************************************************/

#include <CUnit/Basic.h>
#include <stdlib.h>

#include <cq_city_payment/cq_city_payment_core.h>
#include "frcomm.h"
#include "frmem.h"
#include "conntrack.h"
#include "frct_comm.h"
#include "../../../src/module/cq_city_payment/config.h"

extern int cq_city_payment_detect(Conntrack *p_st_conn, Apm_packet *p_st_packet);

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

static void case1(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    i_ret = cq_city_payment_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_NO);

    return;
}

static void case2(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("\x7b\x31\x3a\x20\x30\x30\x30\x32\x37\x33\x42\x37\x35\x32\x20\x20\x20\x20\x33\x32\x33\x36\x35\x33\x30\x31\x30\x30\x31\x35\x30\x30\x31\x36\x35\x33\x30\x39\x39\x39\x39\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x4d\x73\x67\x31\x37\x30\x33\x31\x37\x31\x36\x35\x34\x32\x35\x30\x30\x36\x30\x36\x32\x30\x31\x37\x30\x33\x31\x38\x32\x30\x31\x37\x30\x33\x31\x37\x31\x36\x35\x30\x33\x38\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x7d\x7b\x32\x3a\x3a\x53\x42\x4e\x3a\x33\x32\x33\x36\x35\x33\x30\x31\x30\x30\x31\x35\x3a\x30\x35\x31\x3a\x32\x30\x31\x37\x30\x33\x31\x38\x3a\x4f\x43\x4e\x3a\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x31\x33\x36\x36\x31\x33\x32\x38\x30\x7d\x7b\x43\x3a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x7d\x45\x4e\x44");
    p_st_packet->ui_payload_len = 248;

    i_ret = cq_city_payment_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
    {
        if(p_st_packet->p_payload)
            frfree(p_st_packet->p_payload);
        frfree(p_st_packet);
    }

    return;
}

static void case3(void)
{
    Conntrack *p_st_conn = NULL;
    Apm_packet *p_st_packet = NULL;
    int i_ret = 0;

    p_st_conn = (Conntrack *)frmalloc(sizeof(Conntrack));
    if(p_st_conn == NULL)
        goto ERROR;
    memset(p_st_conn, 0x00, sizeof(Conntrack));

    p_st_packet = (Apm_packet *)frmalloc(sizeof(Apm_packet));
    if(p_st_packet == NULL)
        goto ERROR;
    memset(p_st_packet, 0x00, sizeof(Apm_packet));
    p_st_packet->p_payload = frstrdup("\x7b\x31\x3a\x30\x30\x30\x30\x32\x37\x33\x42\x37\x35\x32\x20\x20\x20\x20\x33\x32\x33\x36\x35\x33\x30\x31\x30\x30\x31\x35\x30\x30\x31\x36\x35\x33\x30\x39\x39\x39\x39\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x4d\x73\x67\x31\x37\x30\x33\x31\x37\x31\x36\x35\x34\x32\x35\x30\x30\x36\x30\x36\x32\x30\x31\x37\x30\x33\x31\x38\x32\x30\x31\x37\x30\x33\x31\x37\x31\x36\x35\x30\x33\x38\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x39\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x7d\x7b\x32\x3a\x3a\x53\x42\x4e\x3a\x33\x32\x33\x36\x35\x33\x30\x31\x30\x30\x31\x35\x3a\x30\x35\x31\x3a\x32\x30\x31\x37\x30\x33\x31\x38\x3a\x4f\x43\x4e\x3a\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x31\x33\x36\x36\x31\x33\x32\x38\x30\x7d\x7b\x43\x3a\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x7d\x45\x4e\x44");
    p_st_packet->ui_payload_len = 248;

    i_ret = cq_city_payment_detect(p_st_conn, p_st_packet);

    CU_ASSERT_EQUAL(i_ret, APM_L7_IS_YES);

    frfree(p_st_conn);
    frfree(p_st_packet->p_payload);
    frfree(p_st_packet);

    return;

ERROR:
    if(p_st_conn)
        frfree(p_st_conn);
    if(p_st_packet)
    {
        if(p_st_packet->p_payload)
            frfree(p_st_packet->p_payload);
        frfree(p_st_packet);
    }

    return;
}

int add_suite_cq_city_payment_detect(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" cq_city_payment_detect()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", case1) == NULL ||
        CU_add_test(pSuite, "case2", case2) == NULL ||
        CU_add_test(pSuite, "case3", case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
